export const insurancesByPageApi = () => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', 'http://127.0.0.1:3002/insurances/getInsurancesByPage', true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}

export const insurancesDelApi = (row) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('PUT', 'http://127.0.0.1:3002/insurances/delInsurances', true)
        xhr.setRequestHeader('Content-Type', "application/json")
        xhr.send(JSON.stringify(row))
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                    console.log("删除返回值:", JSON.parse(xhr.responseText));
                }
            }
        }
    })
}

export const uploadApi = (fileObj) => {
    return new Promise((resolve, reject) => {
        let xhr = new XMLHttpRequest()
        xhr.open('POST', 'http://127.0.0.1:3002/upload/profile', true)
        let formdata = new FormData()
        formdata.append('avatar', fileObj)
        xhr.send(formdata)
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText));
                    console.log("图片上传", JSON.parse(xhr.responseText));
                }
            }
        }
    })
}

export const insurancesAddApi = (row) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('POST', 'http://127.0.0.1:3002/insurances/addInsurances', true)
        xhr.setRequestHeader('Content-Type', "application/json")
        xhr.send(JSON.stringify(row))
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                    console.log("resolve", JSON.parse(xhr.responseText));
                }
            }
        }
    })
}

export const insurancesFixApi = (row) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('PUT', 'http://127.0.0.1:3002/insurances/modifyInsurances', true)
        xhr.setRequestHeader('Content-Type', "application/json")
        xhr.send(JSON.stringify(row))
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                    console.log("fix返回值", JSON.parse(xhr.responseText));
                }
            }
        }
    })
}
// 搜索信息渲染列表
export const insuranceListApi = () => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', 'http://127.0.0.1:3002/insurances/getAllinsurance', true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}
export const searnameApi = (detailName) => {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest()
        xhr.open('GET', `http://127.0.0.1:3002/insurances/searName?insurancesName=${detailName}`, true)
        xhr.send()
        xhr.onreadystatechange = () => {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
            }
        }
    })
}